package com.gs.api.projects.shejiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gs.api.config.Constant;
import com.gs.api.projects.shejiao.entity.TMemberUser;
import com.gs.api.projects.shejiao.entity.TTeamlevel;
import com.gs.api.projects.shejiao.dao.TTeamlevelMapper;
import com.gs.api.projects.shejiao.service.ITMemberUserService;
import com.gs.api.projects.shejiao.service.ITTeamlevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author guoshao
 * @since 2021-10-14
 */
@Service
public class TTeamlevelServiceImpl extends ServiceImpl<TTeamlevelMapper, TTeamlevel> implements ITTeamlevelService {


    @Autowired
    private ITMemberUserService itMemberUserService;

    @Autowired
    private TTeamlevelMapper tTeamlevelMapper;

    /**
     * 获取parentid
     * @param userId 当前用户ID
     * @return
     */
    @Override
    public String getParentId(String userId){
        //获取关系信息
        TTeamlevel teamlevel = this.getOne(new QueryWrapper<TTeamlevel>().eq("user_id",userId));
        if(null != teamlevel && StringUtils.isNotBlank(teamlevel.getParentId())){
            return teamlevel.getParentId();
        }
        return  null;
    }

    @Override
    public List<Map<String, Object>> selectMyTeam(String openid,Integer offset) {
        Integer pageNum = 1;
        if(offset > 0){
            pageNum = (offset / Constant.PAGESIZE) + 1;
        }
        PageHelper.startPage(pageNum, Constant.PAGESIZE).setOrderBy("create_time desc");

        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(tTeamlevelMapper.selectMyTeam(openid));

        return pageInfo.getList();
    }
}
